package com.robot.zxlistview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.MyViewHolder> {
    private List<Bean>beans=new ArrayList<>();
    final String TAG="MainActivity2>>>>MyAdapter2";
    Context mContext;
    public MyAdapter2(List<Bean>beans,Context context){
        this.beans=beans;
        this.mContext=context;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.recyclerview_item, null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tv.setText(beans.get(position).getName());
//        holder.tv.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("LongLogTag")
//            @Override
//            public void onClick(View v) {
//                Log.e(TAG, "onClick: "+position);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.rv_tv);
            itemView.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("LongLogTag")
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListerner!=null){
                        Log.e(TAG, "itemView.setOnClickListener: ");
                        mOnItemClickListerner.onRecyClerTtemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
    private OnRecyclerItemClickListerner mOnItemClickListerner;

    @SuppressLint("LongLogTag")
    public void setRecyclerItemClickListener(OnRecyclerItemClickListerner listener){
        Log.e(TAG, "OnRecyclerItemClickListerner: ");

        mOnItemClickListerner=listener;
    }
    public interface OnRecyclerItemClickListerner{

        void onRecyClerTtemClick(int position);
    }
}
